import board
import digitalio
import time

DCP_SDO = digitalio.DigitalInOut(board.GP14)
DCP_SCLK = digitalio.DigitalInOut(board.GP15)
DCP_CS_N = digitalio.DigitalInOut(board.GP16)

DCP_SDO.direction = digitalio.Direction.INPUT
DCP_SCLK.direction = digitalio.Direction.OUTPUT
DCP_CS_N.direction = digitalio.Direction.OUTPUT

def read_dcp():
    t = 1/(1e7)
    DCP_CS_N.value = 1
    DCP_SCLK.value = 1
    time.sleep(t/2)
    
    data = 0b0000_0000
    for i in range(4):
        DCP_CS_N.value = 0
        DCP_SCLK.value = 1
        time.sleep(t)
        DCP_SCLK.value = 0
        time.sleep(t)
    
    for i in range(8):
        DCP_CS_N.value = 0
        DCP_SCLK.value = 0
        time.sleep(t)
        DCP_SCLK.value = 1
        data_bit = DCP_SDO.value
        data = (data << 1) | data_bit
        time.sleep(t)
    DCP_CS_N.value = 1
    DCP_SCLK.value = 1
    data = data/255*3.3
    return data
    
        

